<?php
    class Login extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            $this->load->library(array('util','session'));
            $this->load->model('user_model','users'); 
        }
        public function index()
        {
            $id_user = $this->session->userdata('id_user');
			$logged_in = $this->session->userdata('logged_in');
			if(isset($id_user) and $logged_in == TRUE)
			{
				redirect('admin/home');
			}	    
        $data = array();    
        $this->load->view('admin/members/login',$data);
        }
        public function process()
        {
            $data = array();
           
			 $username = trim(htmlspecialchars($this->input->post('txt_user')));
			$password = md5(trim($this->input->post('txt_pass')));
			
			if($this->users->check_login($username,$password))
			{
				$q = $this->users->check_login($username,$password);
				foreach($q->result() as $row)
				{
				    $data_user = array(
                        'username' => $row->username,
                        'fullname' => $row->fullname,
                        'logged_in'  => TRUE,
                        'id_user'        => $row->id
                    );
					$this->session->set_userdata($data_user);	
					
				 redirect('admin/home');								
				}
				$data['error'] = "Đăng nhập thành công";
			}
			else
			{
			   $data['error'] = "Yêu cầu đăng nhập hệ thống";		
			}            
            $this->title = "Login";  
            $this->load->view('admin/members/login',$data);
           
           	
        }	
        
    }
?>